package com.chixing.controller;

import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.chixing.config.RSACoder;
import com.chixing.entity.Car;
import com.chixing.entity.Customer;
import com.chixing.entity.LoginCustomer;
import com.chixing.service.AddCustomerService;
import com.chixing.service.ICustomerService;
import com.chixing.util.Result;
import com.chixing.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.security.Key;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shy
 * @since 2022-10-20
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;

    @Autowired
    private AddCustomerService addCustomerService;

//    @RequestMapping(value = "/login", method = RequestMethod.GET)
//    public String login(HttpServletRequest request, Model model) {
//        try {
//            //初始化密钥
//            Map<String, Key> keyMap = RSACoder.initKey();
//            //获取
//            String publicKey = RSACoder.getDataByPublicKey(keyMap);
//            String privateKey = RSACoder.getDataByPrivateKey(keyMap);
//            //存入session
//            HttpSession session = request.getSession();
//            session.setAttribute("publicKey", publicKey);
//            session.setAttribute("privateKey", privateKey);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        //转跳登陆页面
//        return "/login";
    //}


    //登录
    @PostMapping("login")
    @ResponseBody
    public Result login(Customer customer, HttpServletRequest request, HttpServletResponse response){

        //1.mysql验证用户名和密码
        Result result=customerService.login(customer);
        System.out.println("controller login response:"+result);
        System.out.println(result);
        if (result.getCode()==200){
            String token= (String) result.getData();
            System.out.println(token);
            System.out.println("用户登陆成功");
        }


        return result;
    }


    @PostMapping("/regist")

    public ModelAndView regist(Long custTelno , Long custPassword, HttpServletResponse request, HttpServletResponse response){
        ModelAndView mav = new ModelAndView();

        Result result=customerService.regist(custTelno,custPassword);
        System.out.println("controller Regist response:"+request);

        if (result.getCode()==200){
            System.out.println("注册成功");
            mav.addObject("msg",result.getReason());
            mav.setViewName("login/login.jsp");
        }else{
            System.out.println("该手机号已经注册过");
            mav.addObject("msg",result.getReason());
            mav.setViewName("login/regist.jsp");
        }
    return mav;
    }


    //个人中心首页跳转
    @GetMapping("getId/{id}")
    public ModelAndView customerHtml(@PathVariable("id") Integer custId){
        Customer result = customerService.getall(custId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("cust",result);
        mav.setViewName("customer/personalCenter");
        return mav;
    }
//个人中心判断是否为登录状态
    @GetMapping("personalData")
    @ResponseBody
    public Result loadPersonalData(HttpServletRequest request){
        String token = request.getHeader("token");
//        System.out.println("index token:" + token);

        // ajax 请求后端加载: 1. 用户登录数据  2. 热门车辆数据
        LoginCustomer loginCustomer = TokenUtil.parseToken(token);
        Integer customerId = loginCustomer.getCustId();
        Customer customer = addCustomerService.getById(customerId);
        String customerName = customer.getCustName();
        //(3)商品热数据
        Map<String,Object> map =  new HashMap<>();
        map.put("loginCustomer",loginCustomer);
        map.put("customerName",customerName);
        return Result.success("ok",map);
    }

}
